<template>
  <div class="manage">
    <!-- 1.顶部区域 -->
    <div class="header">
      <div class="left">用户名: <input type="text" ref="sousuo" /></div>
      <div class="right">
        <el-button type="primary" size="small" @click="requireManage()"
          >查询</el-button
        >
        <el-button type="primary" size="small" @click="addManage()"
          >添加</el-button
        >
      </div>
    </div>
    <!-- 2.table信息 -->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="" label="id" width="80" align="center">
        <template #default="scope">
          <span>{{ scope.$index + 1 }}</span></template
        >
      </el-table-column>
      <el-table-column
        prop="adminUsername"
        label="用户名"
        width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="adminName"
        label="真实姓名"
        width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="adminSex" label="性别" width="100" align="center">
        <template #default="scope">
          <span>{{ scope.row.adminSex == 1 ? "男" : "女" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="adminTel"
        label="手机号"
        width="180"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="adminTime"
        label="日创建时间"
        width="180"
        align="center"
      >
        <span>{{ this.time }}</span>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="120" align="center">
        <template>
          <el-button
            type="success"
            plain
            size="small"
            width="50"
            align="center"
            @click="qiyong()"
            >启用</el-button
          >
        </template>
      </el-table-column>
      <el-table-column prop="handle" label="操作" align="center">
        <!-- 插槽 -->
        <template #default="scope">
          <el-button size="small" @click="jinyong(scope.row)">禁用</el-button>
          <el-button size="small" @click="editManage(scope.row.adminId)"
            >编辑</el-button
          >
          <el-button type="danger" size="small" @click="delManage(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <div class="block">
      <span class="demonstration"></span>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-change="currentPage1"
        :page-sizes="[2, 4, 6, 8]"
        :page-size="limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import {
  showAdminList,
  delAdminList,
  inquireAdminList,
} from "@/apis/zhuomeng/Index.js";
export default {
  data() {
    return {
      dialogVisible: false,
      tableData: [],
      total: 0,
      page: 1,
      limit: 10,
      currentPage1: 1,
      time: new Date().toLocaleString(),
    };
  },
  methods: {
    // 1.获取管理员列表数据
    showall() {
      showAdminList(this.page, this.limit).then((ok) => {
        console.log(ok);
        this.tableData = ok.data.data.records;
        this.total = ok.data.data.records ? ok.data.data.records.length : 0;
      });
    },
    // 调分页器page和limit，防止total值变化
    showany() {
      showAdminList(this.page, this.limit).then((ok) => {
        console.log(ok);
        this.tableData = ok.data.data.records;
      });
    },
    // 添加
    addManage() {
      this.$router.push("/adminchuangbiane");
    },
    // 启用
    qiyong() {
      this.$message({
        message: "启用成功",
        type: "success",
      });
    },
    // 禁用
    jinyong() {
      this.$message({
        message: "禁用成功",
        type: "success",
      });
    },
    // 编辑
    editManage(id) {
      this.$router.push({
        name: "adminchuangbiane",
        query: { rowId: id },
      });
    },
    // 删除
    delManage(row) {
      this.$confirm("确定删除吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 4.调用删除的接口，删除改行数据
          delAdminList(row.adminId).then((ok) => {
            console.log(ok);
            // 调用show()
            this.showall();
            this.$message({
              type: "success",
              message: "删除成功",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    // 2.查询请求
    requireManage() {
      if (this.$refs.sousuo.value != 0) {
        inquireAdminList(this.$refs.sousuo.value).then((ok) => {
          // console.log(ok.data.data);
          this.tableData = ok.data.data;
          // this.limit = this.tableData.length;
          // this.showall();
        });
      }
    },
    // 分页器
    handleSizeChange(val) {
      this.limit = val;
      // console.log(this.limit);
      this.showany();
    },
    handleCurrentChange(val) {
      this.page = val;
      // console.log(this.page);
      this.showany();
    },
  },
  mounted() {
    //调用获取数据列表
    this.showall();
  },
};
</script>

<style scoped>
#app {
  background: white;
}
.header {
  display: flex;
}
.header input {
  height: 30px;
  border: 1px solid #ccc;
  margin-left: 20px;
}
.header .left {
  margin-right: 30px;
}
.el-table {
  margin-top: 20px;
}
.block {
  margin-top: 30px;
}
</style>
